package edu.kit.aifb.ldstream.repository.ldcrawler;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashSet;
import java.util.Set;

import org.semanticweb.yars.nx.Node;

import com.ontologycentral.ldspider.frontier.Frontier;
import com.ontologycentral.ldspider.hooks.links.LinkFilterDefault;

/**
 * This LinkFilter follows only the given URIs. Other will be ignored.
 * 
 * @author Holger
 *
 */
public class LinkFilterAllowURI extends LinkFilterDefault {

	private Set<URI> allowedURIs = new HashSet<URI>();

	/**
	 * Creates a new LinkFilter with an empty allowedURI set.
	 * 
	 * @param frontier to be added.
	 */
	public LinkFilterAllowURI(Frontier frontier) {
		super(frontier);
	}

	/**
	 * Creates a new LinkFilter with a initial allowedUri set. Additional URIs can be set afterwards.
	 * 
	 * @param frontier to be added.
	 * @param uris adding this URIs as the allowed URIs which are being followed.
	 */
	public LinkFilterAllowURI(Frontier frontier, URI... uris){
		super(frontier);
		for(URI uri : uris){
			allowedURIs.add(uri);
		}
	}

	/**
	 * Adding new URIs to the allowedURI set, which will be allowed to be crawled.
	 * @param uri
	 */
	public synchronized void addAllowedURI(URI uri){
		this.allowedURIs.add(uri);
	}
	
	/** {@inheritDoc} */
	public synchronized void processStatement(Node[] nx) {

		if(nx != null){
			URI u = null;
			try {
				// seems to be the predicate
				u = new URI(nx[1].toString());
			} catch (URISyntaxException e) {
				e.printStackTrace();
			}
			// only process the node if the predicate is an allowed URI
			if(this.allowedURIs.contains(u)){
			//	System.out.println("Following " + nx[0].toString() + "  =>  " + nx[2].toString());
				super.processStatement(nx);
			}
		}

	}



}
